package com.zte.sys.login.control;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.sys.login.model.*;
import com.zte.sys.login.service.UserLoginService;
import com.zte.util.LogOutException;
import com.zte.util.OperatorLog;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Controller
@EnableScheduling
public class Action {





    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private RedisTemplate redisTemplate;


//    @Scheduled(fixedRate=1000) //1000毫秒执行一次
//    public  void  printTime(){
//
//        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
//        String date = dateFormat.format(new Date());
//        String userId = WebSocketServer.getUserId();
//        if(userId.length()<1){
//            return;
//        }
//        LUsers user =(LUsers) redisTemplate.opsForValue().get(userId);
//        LUsers users = userLoginService.detailOfUser(user);
//        if(user.getPermissions().size()!=users.getPermissions().size()){
//            redisTemplate.opsForValue().set(userId, users, 30, TimeUnit.MINUTES);
//            WebSocketServer.sendInfo(date,userId);
//        }
//    }





    @RequestMapping("/main")
    public String main(){
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        String id = request.getParameter("id");
//        dd = id;
//        System.out.println(dd);
        return "main";
    }
    @RequestMapping("/left")
    public String left(){
        return "left";
    }
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    @RequestMapping("/top")
    public String top(){
        return "top";
    }
    @RequestMapping("/footer")
    public String footer(){
        return "footer";
    }
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    @RequestMapping("sys/journal/logView")
    public String logView(){
        return "sys/logs/logView";
    }


    @RequestMapping("/queryForLogin")
    @ResponseBody
    public ResultMessage queryForLogin(LUsers user) throws Exception{
        ResultMessage resultMessage = new ResultMessage();
        LUsers users = userLoginService.detailOfUser(user);
        long statusId = users.getStatusId();
        if(statusId==2){
            throw new LogOutException("无权限");
        }
        if(users!=null){
            String userId = String.valueOf(users.getUId());
            long uId = users.getUId();
            user.setUId(uId);
            user.setUpassword(null);
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            HttpSession session = request.getSession();
            session.setAttribute("id",userId);

            redisTemplate.opsForValue().set(userId, users, 300, TimeUnit.MINUTES);
            resultMessage.setStatus(200);
            resultMessage.setMsg("登录成功");
            resultMessage.setData(user);
            return resultMessage;
        }
        resultMessage.setStatus(500);
        resultMessage.setMsg("账号或密码错误，请重新登录");
        return resultMessage;
    }

    @RequestMapping("/queryModule")
    @ResponseBody
    public List<Module> queryModule(String id){
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        HttpSession session = request.getSession();
//        String id =(String) session.getAttribute("id");
        LUsers user =(LUsers) redisTemplate.opsForValue().get(id);
        List<Permission> permissions = user.getPermissions();
        List<Module> modules = userLoginService.detailOfModule(permissions);
        return modules;
    }

    @RequestMapping("sys/journal/queryLogs")
    @ResponseBody
    @OperatorLog(operate="日志查询", module="日志管理")
    public PageInfo<Journal> queryLogs(String pageNum, QueryCondition queryCondition, String uname){
//        ConcurrentHashMap<String, WebSocketServer> webSocketMap = WebSocketServer.getWebSocketMap();

        int pageNumm = 1;
        if(pageNum!=null && pageNum!=""){
            pageNumm = Integer.parseInt(pageNum);
        }
        PageInfo<Journal> pageInfo = userLoginService.queryLogs(pageNumm,queryCondition,uname);
        return pageInfo;
    }

    @RequestMapping("/quit")
    @ResponseBody
    public String quit(String id){
        redisTemplate.delete(id);
        return "已退出";
    }





    @RequestMapping("sys/users/findByPage")
    public String userList(){
        return "sys/users/userList";
    }
    @RequestMapping("sys/dept/findByPage")
    public String deptList(){
        return "sys/dept/deptList";
    }
    @RequestMapping("sys/job/findByPage")
    public String positionList(){
        return "sys/position/positionList";
    }
    @RequestMapping("sys/modules/list")
    public String moduleList(){
        return "sys/modules/moduleList";
    }
    @RequestMapping("sys/journal/list")
    public String logList(){
        return "sys/logs/logList";
    }
    @RequestMapping("sys/area/list")
    public String areaList(){
        return "sys/area/areaList";
    }

}
